﻿namespace N_dimenslArr
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using Tao.FreeGlut;
    using Tao.OpenGl;
    using Tao.Platform;

    public class Grid
    {
        private int squareSize;
        private int viewportWidth;
        private int viewportHeight;
        private double scaleRatio;
        private string stringFormat;

        public Grid()
        { 
        }

        public Grid(int squareSize, int viewportWidth, int viewportHeight, double scaleRatio)
        {
            Debug.Assert(squareSize != 0, "squareSize != 0");
            Debug.Assert(viewportWidth > 0, "viewportWidth > 0");
            Debug.Assert(viewportHeight > 0, "viewportHeight > 0");
            Debug.Assert(scaleRatio > 0, "scaleRatio > 0");
            this.squareSize = squareSize;
            this.viewportWidth = viewportWidth;
            this.viewportHeight = viewportHeight;
            this.scaleRatio = scaleRatio * 2;
            this.stringFormat = "0.0";
        }

        public void Render(double[] shift, double zoom)
        {
            Gl.glPushAttrib(Gl.GL_TRANSFORM_BIT);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glPushMatrix();
            Gl.glLoadIdentity();
            Gl.glOrtho(0, this.viewportWidth, this.viewportHeight, 0, -1000, 1000);

            double x = 0;
            double y = 0;
            for (int i = 0; i <= ((this.viewportWidth / this.squareSize) - 1); ++i)
            {
                if ((i % 2) == 0)
                {
                    Gl.glColor4f(0, 0, 0, 1.0f);
                }
                else 
                {
                    Gl.glColor4f(0, 0, 0, 0.5f);
                }

                Gl.glBegin(Gl.GL_LINES);
                Gl.glVertex3d(x, 0, 0);
                Gl.glVertex3d(x, this.viewportHeight, 0);
                Gl.glEnd();

                Gl.glColor4f(0, 0, 0, 1.0f);
                Gl.glRasterPos2d(x, this.viewportHeight);
                
                this.stringFormat = "0.00";
                if (zoom > 4)
                {
                    this.stringFormat += '0';
                    if (zoom > 8)
                    {
                        this.stringFormat += '0';
                    }
                }

                foreach (char char_for_draw in (((x - shift[0]) / zoom) / this.scaleRatio).ToString(this.stringFormat))
                {
                    Glut.glutBitmapCharacter(Glut.GLUT_BITMAP_9_BY_15, char_for_draw);
                }

                x += this.squareSize;
            }

            for (int i = 0; i <= ((this.viewportHeight / this.squareSize) - 1); ++i)
            {
                if ((i % 2) == 0)
                {
                    Gl.glColor4f(0, 0, 0, 1.0f);
                }
                else
                {
                    Gl.glColor4f(0, 0, 0, 0.5f);
                }

                Gl.glBegin(Gl.GL_LINES);
                Gl.glVertex3d(0, y, 0);
                Gl.glVertex3d(this.viewportWidth, y, 0);
                Gl.glEnd();

                Gl.glColor4f(0, 0, 0, 1.0f);
                Gl.glRasterPos2d(this.viewportWidth - 56, y + 10);
                foreach (char char_for_draw in (((y - shift[1]) / zoom) / this.scaleRatio).ToString(this.stringFormat))
                {
                    Glut.glutBitmapCharacter(Glut.GLUT_BITMAP_9_BY_15, char_for_draw);
                }

                y += this.squareSize;
            }
           
            Gl.glEnd();
            Gl.glPopMatrix();
            Gl.glPopAttrib();
        }
    }
}